package org.csu.mypetstore.persistence.impl;

import org.csu.mypetstore.domain.LineItem;
import org.csu.mypetstore.persistence.LineItemDao;
import org.csu.mypetstore.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class LineItemDaoImpl implements LineItemDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<LineItem> getLineItemsByOrderId(int orderId) {
        String sql = "  SELECT\n" +
                "      ORDERID,\n" +
                "      LINENUM AS lineNumber,\n" +
                "      ITEMID,\n" +
                "      QUANTITY,\n" +
                "      UNITPRICE\n" +
                "    FROM LINEITEM\n" +
                "    WHERE ORDERID = ?";
        return template.query(sql,new BeanPropertyRowMapper<LineItem>(LineItem.class),orderId);
    }

    @Override
    public void insertLineItem(LineItem lineItem) {
        String sql = "INSERT INTO LINEITEM (ORDERID, LINENUM, ITEMID, QUANTITY, UNITPRICE)\n" +
                "    VALUES (?, ?, ?, ?, ?)";
        template.update(sql,lineItem.getOrderId(),lineItem.getLineNumber(),lineItem.getItemId(),lineItem.getQuantity(),lineItem.getUnitPrice());
    }
}
